(47, )
, you need to reshape it into (47,1)
to make sure everything goes alrightnp.array([1,2,3,4])
is (4, )
, this is column vector. Be aware of the shape of data.
In [1]:
%reload_ext autoreload
%autoreload 2
%matplotlib inline
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import sys
sys.path.append('..')
from helper import linear_regression as lr # my own module
from helper import general as general
import tensorflow as tf
In [2]:
raw_data = pd.read_csv('ex1data2.txt', names=['square', 'bedrooms', 'price'])
data = general.normalize_feature(raw_data)
print(data.shape)
data.head()
Out[2]:
In [3]:
X_data = general.get_X(data)
print(X_data.shape, type(X_data))
y_data = general.get_y(data).reshape(len(X_data), 1) # special treatment for tensorflow input data
print(y_data.shape, type(y_data))
In [4]:
epoch = 2000
alpha = 0.01
In [8]:
optimizer_dict={'GD': tf.train.GradientDescentOptimizer,
'Adagrad': tf.train.AdagradOptimizer,
'Adam': tf.train.AdamOptimizer,
'Ftrl': tf.train.FtrlOptimizer,
'RMS': tf.train.RMSPropOptimizer
}
results = []
for name in optimizer_dict:
res = lr.linear_regression(X_data, y_data, alpha, epoch, optimizer=optimizer_dict[name])
res['name'] = name
results.append(res)
In [9]:
fig, ax = plt.subplots(figsize=(16, 9))
for res in results:
loss_data = res['loss']
# print('for optimizer {}'.format(res['name']))
# print('final parameters\n', res['parameters'])
# print('final loss={}\n'.format(loss_data[-1]))
ax.plot(np.arange(len(loss_data)), loss_data, label=res['name'])
ax.set_xlabel('epoch', fontsize=18)
ax.set_ylabel('cost', fontsize=18)
ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax.set_title('different optimizer', fontsize=18)
Out[9]:
In [ ]: